package com.jarvis.pzz;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.jarvis.pzz.common.BaseFragment;
import com.jarvis.pzz.models.VCModel;
import com.jarvis.pzz.util.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by jarvis.liu on 2016-11-02.
 */

public class DemoFragment extends BaseFragment implements View.OnClickListener {
    @BindView(R.id.rel_left)
    RelativeLayout rel_left;
    @BindView(R.id.rel_right)
    RelativeLayout rel_right;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_test)
    TextView tv_test;
    @BindView(R.id.lin_title)
    LinearLayout lin_title;

    @BindView(R.id.recy_vc)
    RecyclerView recy_vc;
    @BindView(R.id.ptr_vc)
    PtrClassicFrameLayout ptr_vc;
    private VcAdapter vcAdapter;

    private int pageSize = 10;
    private int currentPage = 1;
    private int maxPage = 1;


    public static DemoFragment newInstance() {
        Bundle args = new Bundle();
        DemoFragment fragment = new DemoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_demo;
    }

    @Override
    public void initData() {
        ptr_vc.postDelayed(
                new Runnable() {
                    @Override
                    public void run() {
                        ptr_vc.autoRefresh();
                    }
                }, 100);
    }
    private void setBar(){
        lin_title.setVisibility(View.VISIBLE);
        lin_title.getBackground().setAlpha(255);
        int statusBarHeight1 = -1;
//获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(lin_title.getLayoutParams());
        lp.setMargins(0, statusBarHeight1, 0, 0);
        lin_title.setLayoutParams(lp);
    }
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            //setStatus(false);
            setBar();
        }

    }

    @Override
    protected void initView() {
        setBar();
//        setStatus(false);
        setAdapter();
        setRefresh();
        setListener();
    }

    /**
     * 设置监听事件
     */
    public void setListener() {
        rel_right.setOnClickListener(this);
        rel_left.setOnClickListener(this);
        tv_title.setOnClickListener(this);
        recy_vc.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                VCModel vcModel = vcAdapter.getData().get(i);
                showTextToast("SimpleOnItemClick---------");
            }

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemChildClick(adapter, view, position);
                VCModel vcModel = vcAdapter.getData().get(position);
                switch (view.getId()) {
                    case R.id.tv_phone:
                        showTextToast("onItemChildClick---------");
                        break;
                }
            }
        });

    }

    /**
     * 设置下拉刷新
     */
    public void setRefresh() {
        ptr_vc.setMode(PtrFrameLayout.Mode.BOTH);
        ptr_vc.disableWhenHorizontalMove(true);
        ptr_vc.setLastUpdateTimeRelateObject(this);
        ptr_vc.setPtrHandler(new PtrDefaultHandler2() {

            @Override
            public boolean checkCanDoLoadMore(PtrFrameLayout frame, View content, View footer) {
                return PtrDefaultHandler2.checkContentCanBePulledUp(frame, recy_vc, footer);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler2.checkContentCanBePulledDown(frame, recy_vc, header);
            }

            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                if (currentPage < maxPage) {
                    currentPage++;
                    loadData();
                } else {
                    ptr_vc.refreshComplete();
                }
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                vcAdapter.setNewData(new ArrayList<VCModel>());
                currentPage = 1;
                loadData();
            }
        });
    }

    private void loadData() {
        tv_title.setText("测试");
        tv_test.setText("当前页:"+currentPage);
        showTextToast("当前页:"+currentPage);
        int total_count = 100;
        maxPage = total_count % pageSize == 0 ? total_count / pageSize : total_count / pageSize + 1;
        List<VCModel> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            VCModel v = new VCModel("a" + i, "b" + i, "c" + i, "d" + i, "e" + i, "f" + i, "g" + i, "h" + i);
            list.add(v);
        }
        vcAdapter.addData(list);
        ptr_vc.refreshComplete();
    }

    /**
     * 设置adapter
     */
    public void setAdapter() {
        vcAdapter = new VcAdapter(R.layout.item_vc, new ArrayList<VCModel>());
        recy_vc.setLayoutManager(new LinearLayoutManager(getActivity()));
        recy_vc.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST, getResources().getDrawable(R.drawable.divier_order_gray)));
        vcAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        recy_vc.setAdapter(vcAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_title:

                break;

        }
    }
}
